package edu.nf.project.webs.storeOperation;

import edu.nf.project.entitys.entity.storeOperation.convention.operate.Board;
import edu.nf.project.services.exception.derivedAnomalies.CustomerOrderException;
import edu.nf.project.services.response.BaseController;
import edu.nf.project.services.response.ResultVO;
import edu.nf.project.services.storeOperation.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 餐桌管理控制器
 */
@RestController
@RequestMapping("/api/board")
public class BoardController extends BaseController {
    @Autowired
    private BoardService boardService;

    /**
     * 获取指定区域下属餐桌信息
     */
    @GetMapping
    public ResultVO<List<Board>> getAllBoard(Integer[] regionIds) {
        try {
            List<Board> boards = boardService.getTargetBoardList(regionIds);
            return success(boards);
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(80000, "系统错误，请稍后重试");
        }
    }

    @GetMapping("/{boardId}")
    public ResultVO<Board> getBoard(@PathVariable Integer boardId) {
        try{
            Board board = boardService.getBoardById(boardId);
            return success(board);
        }catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(80000, "系统错误，请稍后重试");
        }
    }

    /**
     * 添加餐桌
     * @return 是否成功
     */
    @PostMapping("/add")
    public ResultVO<Boolean> addBoard(@RequestBody Board board) {
        try {
            return success(boardService.addBoard(board));
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(80000, "系统错误，请稍后重试");
        }
    }

    /**
     * 编辑餐桌
     * @return 是否成功
     */
    @PostMapping("/upd")
    public ResultVO<Boolean> updBoard(@RequestBody Board board) {
        try {
            return success(boardService.updBoard(board));
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(80000, "系统错误，请稍后重试");
        }
    }

    /**
     * 删除餐桌
     * @return 是否成功
     */
    @PostMapping("/del")
    public ResultVO<Boolean> delBoard(Integer boardId) {
        try {
            return success(boardService.delBoard(boardId));
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(80000, "系统错误，请稍后重试");
        }
    }

    /**
     * 更换餐桌
     * @param boardId 新餐桌ID
     * @param orderId 订单ID
     * @return 是否成功
     */
    @PostMapping("/change")
    public ResultVO<Boolean> changeBoard(@RequestParam Integer boardId, @RequestParam Integer orderId) {
        try {
            return success(boardService.changeBoard(boardId, orderId));
        } catch (Exception e) {
            if (e instanceof CustomerOrderException) {
                throw e;
            }
            throw new CustomerOrderException(80000, "系统错误，请稍后重试");
        }
    }
}
